
<?php
/*
  级联类别，可指定表名
  表格字段：id,title,pid
*/
require_once('./Base.php');
$base = new Base();
$db = $base->getDB();

@$table = $_GET['table'];
if (!isset($table)) {
  $table = 'rywtypes';
}

$sql = "select * from $table where pid=0";
$list = $db->rawQuery($sql);

$res = array();

foreach ($list as $key => $value) {
  $json = array();
  $children = array();
  $json["id"] = $value["id"];
  $json["title"] = $value["title"];

  $tmpId = $value["id"];
  $sql2 = "select * from $table where pid=$tmpId";
  $list2 = $db->rawQuery($sql2);

  foreach ($list2 as $key => $value) {
    $json2 = array();
    $children2 = array();
    $json2["id"] = $value["id"];
    $json2["title"] = $value["title"];

    $tmpId = $value["id"];
    $sql3 = "select * from $table where pid=$tmpId";
    $list3 = $db->rawQuery($sql3);

    foreach ($list3 as $key => $value) {
      $json3 = array();
      $json3["id"] = $value["id"];
      $json3["title"] = $value["title"];
      $children2[] = $json3;
      $json2["children"] = $children2;
    }
    $children[] = $json2;
    $json["children"] = $children;
  }

  $res[] = $json;
}

echo json_encode($res);
